package com.mhy.netsafe.utils;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Dns;

/**
 * 项目名 NetSafe
 * 所在包 com.mhy.netsafe.utils
 * 作者 mahongyin
 * 时间 2019/8/11 16:58
 * 邮箱 mhy.work@qq.com
 * 描述 说明:判断网络状态、代理
 */
public class HttpUtils {

    private boolean isWifiProxy(Context context) {
        final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
        String proxyAddress;
        int proxyPort;
        if (IS_ICS_OR_LATER) {
            proxyAddress = System.getProperty("http.proxyHost");
            String portStr = System.getProperty("http.proxyPort");
            proxyPort = Integer.parseInt((portStr != null ? portStr : "-1"));
        } else {
            proxyAddress = android.net.Proxy.getHost(context);
            proxyPort = android.net.Proxy.getPort(context);
        }
        return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1);
    }

    /***
     * if(isWifiProxy()){  //true,使用了wifi代理
     *
     *     //不做访问操作
     *
     * }else{                 //flase,正常用户，未使用wifi代理
     *
     *    //访问数据
     *
     * }
     */

    class TestDns implements Dns {

        @Override
        public List<InetAddress> lookup(String hostname) throws UnknownHostException {
            List<InetAddress>inetAddress= new ArrayList<>();
            List<InetAddress>hostInetAddressname=null;
            try {
                hostInetAddressname=Dns.SYSTEM.lookup(hostname);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            if (hostInetAddressname!=null&&hostInetAddressname.size()>0){
                inetAddress.addAll(hostInetAddressname);
            }
            try {
                inetAddress.add(InetAddress.getByName("47.94.95.69"));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            return inetAddress;
        }
    }
}
